package com.bocai.utils;


import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by tu_pei on 2017/7/25.
 */
public class DateUtils {
    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static String HH_MM_SS = "HH:mm:ss";
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * 获得默认的 date pattern
     */
    public static String getDatePattern()
    {
        return YYYY_MM_DD_HH_MM_SS;
    }

    /**
     * 返回预设Format的当前日期字符串
     */
    public static String getToday(){
        Date today = new Date();
        return format(today);
    }

    /**
     * 使用预设Format格式化Date成字符串
     */
    public static String format(Date date)
    {
        return date == null ? " " : format(date, getDatePattern());
    }

    /**
     * 使用参数Format格式化Date成字符串
     */
    public static String format(Date date, String pattern){
        return date == null ? " " : new SimpleDateFormat(pattern).format(date);
    }
    /**
     * 使用预设格式将字符串转为Date
     */
    public static Date parse(String strDate) throws ParseException{
        return StringUtils.isBlank(strDate) ? null : parse(strDate,
                getDatePattern());
    }

    /**
     * 使用参数Format将字符串转为Date
     */
    public static Date parse(String strDate, String pattern)
            throws ParseException{
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return StringUtils.isBlank(strDate) ? null : format.parse(strDate);
    }

    /**
     * 在日期上增加数个整月
     */
    public static String addMonth(Date date, int n){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, n);
        return format(cal.getTime(),YYYY_MM_DD);
    }
    /**
     * 获取当天最后一秒
     * @param date
     * @return
     */
    public static Date getDayLastSecond(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }
    /**
     * 日期增加
     * @param date
     * @param n
     * @return
     */
    public static String addDay(Date date, int n){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH,cal.get(Calendar.DAY_OF_MONTH)+n);//让日期加1
        return format(cal.getTime(),YYYY_MM_DD);
    }

    public static String getLastDayOfMonth(String year, String month){
        Calendar cal = Calendar.getInstance();
        // 年
        cal.set(Calendar.YEAR, Integer.parseInt(year));
        // 月，因为Calendar里的月是从0开始，所以要-1
        // cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        // 日，设为一号
        cal.set(Calendar.DATE, 1);
        // 月份加一，得到下个月的一号
        cal.add(Calendar.MONTH, 1);
        // 下一个月减一为本月最后一天
        cal.add(Calendar.DATE, -1);
        return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));// 获得月末是几号
    }

    public static Date getDate(String year, String month, String day)
            throws ParseException{
        String result = year + "- "
                + (month.length() == 1 ? ("0 " + month) : month) + "- "
                + (day.length() == 1 ? ("0 " + day) : day);
        return parse(result);
    }

    /**
     * 获取当前时间
     * @return
     */
    public static Date getStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /**
     * 获取当前最后一秒
     * @return
     */
    public static Date getEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }

    /**
     * 获取本周最后一天
     * @param day
     * @return
     */
    public static String getWeekLastDay(String day){
        Calendar cal = Calendar.getInstance();

        try {
            cal.setTime(new SimpleDateFormat(YYYY_MM_DD).parse(day));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        int d = 0;
        if(cal.get(Calendar.DAY_OF_WEEK)==1){
            d = -6;
        }else{
            d = 2-cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        cal.add(Calendar.DAY_OF_WEEK, 6);
        return new SimpleDateFormat(YYYY_MM_DD).format(cal.getTime());
    }
    /**
     * 获取本周第一天
     * @param day
     * @return
     */
    public static String getWeekFristDay(String day) {
        Calendar cal = Calendar.getInstance();

        try {
            cal.setTime(new SimpleDateFormat(YYYY_MM_DD).parse(day));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        int d = 0;
        if(cal.get(Calendar.DAY_OF_WEEK)==1){
            d = -6;
        }else{
            d = 2-cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        return new SimpleDateFormat(YYYY_MM_DD).format(cal.getTime());
    }

    /**
     * 获取本月最后一天
     * @return
     */
    public static String getMonthsLastDay(String day) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(new SimpleDateFormat(YYYY_MM_DD).parse(day));
        }catch (Exception ex){
            ex.printStackTrace();
        }
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date firstDayOfMonth = calendar.getTime();
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return format(calendar.getTime(),YYYY_MM_DD);
    }
    /**
     * 获取本月第一天
     * @return
     */
    public static String getMonthsFristDay(String day) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(new SimpleDateFormat(YYYY_MM_DD).parse(day));
        }catch (Exception ex){
            ex.printStackTrace();
        }
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date firstDayOfMonth = calendar.getTime();
        calendar.add(Calendar.MONTH, 0);
        return format(calendar.getTime(),YYYY_MM_DD);
    }
    /**
     * 取得当月天数
     * */
    public static int getCurrentMonthLastDay(String day)
    {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(new SimpleDateFormat(YYYY_MM_DD).parse(day));
        }catch (Exception ex){
            ex.printStackTrace();
        }
        calendar.set(Calendar.DATE, 1);//把日期设置为当月第一天
        calendar.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        int maxDate = calendar.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 获取两个时间间隔多少天
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getDateDifference(Date startDate,Date endDate){
        long l = endDate.getTime()-startDate.getTime();

        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");//如2016-08-10 20:40
        String fromDate = simpleFormat.format(startDate);
        String toDate = simpleFormat.format(endDate);
        try{
            long from = simpleFormat.parse(fromDate).getTime();
            long to = simpleFormat.parse(toDate).getTime();
            return (int) ((to - from)/(1000 * 60 * 60 * 24)) + 1;
        }catch (Exception e){
        }
        return 0;
    }

    public static Date getMonthStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int index = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.add(Calendar.DATE, (1 - index));
        return calendar.getTime();
    }

    public static Date getMonthEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        int index = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.add(Calendar.DATE, (-index));
        return calendar.getTime();
    }

    public static Date getNext(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        return calendar.getTime();
    }

    public static List<String> getMonthDateList(Date d){
        Date date = DateUtils.getMonthStart(d);
        Date monthEnd = DateUtils.getMonthEnd(d);
        List<String> dateList = new ArrayList<>();
        while (!date.after(monthEnd)) {
            dateList.add(sdf.format(date));
            date = DateUtils.getNext(date);
        }
        return dateList;
    }

    public static void main(String args[]){
        System.out.println(addDay(new Date(),-30));
        System.out.println(getMonthEnd(new Date()));
    }
}
